﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using YunCMS.Common;
using YunCMS.Model;

namespace YunCMS.Controls
{
    public class AuthPage : System.Web.UI.Page
    {
        public AuthPage()
        {
            //this.Load += new EventHandler(AuthPage_Load);
        }

        private void AuthPage_Load(object sender, EventArgs e)
        {
            if (!checkLogin())
            {
                JScript.Alert("没有登陆", this);
                Response.End();
            }
            if (!CheckAuthority())
            {
                JScript.Alert("没有权限", this);
                Response.End();
            }
        }

        private bool checkLogin()
        {
            return Session["username"] != null;
        }

        /// <summary>
        /// Do auth by the module name
        /// </summary>        
        protected virtual string Module
        {
            get { return string.Empty; }
        }

        protected virtual bool CheckAuthority()
        {
            if (Session[Const.USER_AUTHORITY] != null)
            {
                var role = Session[Const.USER_AUTHORITY] as YCUserRole;
                if (role != null)
                {
                    if (role.Type == 1)//super admin
                    {
                        return true;
                    }
                    if (role.Actions.Count > 0)
                    {
                        foreach (var action in role.Actions)
                        {
                            if (this.Module == action.Module)
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
}
